
package com.wyzz.hr.base.wf;

import java.util.*;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.base.wf.leaverequest.LrWorkflow;
import com.wyzz.hr.base.wf.overtimerequest.OrWorkflow;
import com.wyzz.hr.base.wf.recruitrequest.RecWorkflow;
import com.wyzz.hr.base.wf.trainrequest.TrWorkflow;
import com.wyzz.hr.bo.ILeaverequestBO;
import com.wyzz.hr.bo.IOvertimerequestBo;
import com.wyzz.hr.bo.IOvertimetxusedBO;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.bo.IRecruitplanBo;
import com.wyzz.hr.bo.ITremployeeplanBO;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.bo.impl.ExaminDateUtil;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Leaverequest;
import com.wyzz.hr.pojo.Overtimerequest;
import com.wyzz.hr.pojo.Recruitplan;
import com.wyzz.hr.pojo.Trcourseplan;
import com.wyzz.hr.pojo.Tremployeeplan;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.StringUtil;


public class ExaminWFApprover extends AbstractWfApproverAction {

	public ExaminWFApprover() {
	}

	public String DWRwfObjOpSingle(String operate, String comment,
			String employeeId, String objId, String securityNo, String flowType) {
		String msgNoParam = "NOPARAM:{0}\u53C2\u6570\u9519\u8BEF\uFF01";
		String msgNoComment = "NOCOMMENT:\u62D2\u7EDD{0}\u5FC5\u987B\u586B\u5199\u5907\u6CE8\uFF01";
		String msgSecNoErr = "SECNOERR:\u9A8C\u8BC1\u7801\u9519\u8BEF\uFF01\u8BF7\u68C0\u67E5\u662F\u5426\u5DF2\u7ECF\u6267\u884C\u8FC7\u8BE5\u64CD\u4F5C\uFF01";
		String msgStatusErr = "STATUSERR:{0}\u72B6\u6001\u9519\u8BEF\uFF01\u8BF7\u68C0\u67E5\u662F\u5426\u5DF2\u7ECF\u6267\u884C\u8FC7\u8BE5\u64CD\u4F5C\uFF01";
		String msgNoCharge = "NOAUTH:\u60A8\u65E0\u6743\u5904\u7406{0}\u7684{1}\uFF01";
		String msgNoChargeRecp = "NOAUTH:\u60A8\u65E0\u6743\u5904\u7406\u8BE5\u62DB\u8058\u7533\u8BF7\uFF01";
		String msgFail = "FAIL:{0}";
		String msgSUCC = "SUCC:{0}";
		if (StringUtils.isEmpty(operate) || !"approve".equals(operate)
				&& !"reject".equals(operate))
			return StringUtil.message(msgNoParam,
					new Object[] { "\u64CD\u4F5C\u7801" });
		if (StringUtils.isEmpty(employeeId))
			return StringUtil.message(msgNoParam,
					new Object[] { "\u64CD\u4F5C\u8005" });
		if (StringUtils.isEmpty(objId) || StringUtils.isEmpty(securityNo))
			return StringUtil.message(msgNoParam,
					new Object[] { "\u8BF7\u5047\u5355" });
		if (StringUtils.isEmpty(objId) || StringUtils.isEmpty(securityNo))
			return StringUtil.message(msgNoParam,
					new Object[] { "\u9A8C\u8BC1\u7801" });
		if ("reject".equals(operate) && StringUtils.isEmpty(comment))
			return StringUtil.message(msgNoComment,
					new Object[] { "\u8BF7\u5047\u5355" });
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		Employee currentEmp = posBo.getEmpWithPos(employeeId, new String[0]);
		if (currentEmp == null)
			return StringUtil.message(msgNoParam,
					new Object[] { "\u64CD\u4F5C\u8005" });
		Object obj = null;
		String nextApprover = null;
		if ("leaverequest".equals(flowType)) {
			ILeaverequestBO lr_BO = (ILeaverequestBO) getBean("leaverequestBO");
			Leaverequest lr = lr_BO.loadLeaverequest(objId);
			if (lr == null)
				return StringUtil.message(msgNoParam,
						new Object[] { "\u8BF7\u5047\u5355" });
			if (!lr.getLrSecurityNo().equals(securityNo))
				return StringUtil.message(msgSecNoErr, new Object[0]);
			lr.setLrLastChangeBy(currentEmp);
			lr.setLrLastChangeTime(new Date());
			obj = lr;
			nextApprover = lr.getLrNextApprover();
			if (!Status.HR_UPD_SET.contains(lr.getLrStatus()))
				return StringUtil.message(msgStatusErr,
						new Object[] { "\u8BF7\u5047\u5355" });
		} else if ("overtimerequest".equals(flowType)) {
			IOvertimerequestBo or_Bo = (IOvertimerequestBo) getBean("overtimerequestBO");
			Overtimerequest or = or_Bo.loadOvertimerequest(objId);
			if (or == null)
				return StringUtil.message(msgNoParam,
						new Object[] { "\u52A0\u73ED\u5355" });
			if (!or.getOrSecurityNo().equals(securityNo))
				return StringUtil.message(msgSecNoErr, new Object[0]);
			or.setOrLastChangeBy(currentEmp);
			or.setOrLastChangeTime(new Date());
			obj = or;
			nextApprover = or.getOrNextApprover();
			if (!Status.HR_UPD_SET.contains(or.getOrStatus()))
				return StringUtil.message(msgStatusErr,
						new Object[] { "\u52A0\u73ED\u5355" });
		} else if ("trainrequest".equals(flowType)) {
			ITremployeeplanBO tr_BO = (ITremployeeplanBO) getBean("tremployeeplanBO");
			String fetch[] = {
					Tremployeeplan.PROP_TRP_CREATE_BY,
					(new StringBuilder()).append(
							Tremployeeplan.PROP_TRP_CREATE_BY).append(".")
							.append(Employee.PROP_EMP_LOCATION_NO).toString(),
					(new StringBuilder()).append(
							Tremployeeplan.PROP_TRP_CREATE_BY).append(".")
							.append(Employee.PROP_EMP_DEPT_NO).toString(),
					Tremployeeplan.PROP_TRP_LAST_CHANGE_BY,
					(new StringBuilder()).append(
							Tremployeeplan.PROP_TRP_LAST_CHANGE_BY).append(".")
							.append(Employee.PROP_EMP_LOCATION_NO).toString(),
					(new StringBuilder()).append(
							Tremployeeplan.PROP_TRP_LAST_CHANGE_BY).append(".")
							.append(Employee.PROP_EMP_DEPT_NO).toString(),
					Tremployeeplan.PROP_TRP_TRAINEE_NO,
					(new StringBuilder()).append(
							Tremployeeplan.PROP_TRP_TRAINEE_NO).append(".")
							.append(Employee.PROP_EMP_LOCATION_NO).toString(),
					(new StringBuilder()).append(
							Tremployeeplan.PROP_TRP_TRAINEE_NO).append(".")
							.append(Employee.PROP_EMP_DEPT_NO).toString(),
					Tremployeeplan.PROP_TRP_TRAINEE_DEPT,
					Tremployeeplan.PROP_TRP_TRAINEE_LOCATION,
					Tremployeeplan.PROP_TRP_TRCP,
					(new StringBuilder()).append(Tremployeeplan.PROP_TRP_TRCP)
							.append(".").append(
									Trcourseplan.PROP_TRCP_COURSE_NO)
							.toString(),
					(new StringBuilder()).append(Tremployeeplan.PROP_TRP_TRCP)
							.append(".")
							.append(Trcourseplan.PROP_TRCP_TRINS_ID).toString(),
					(new StringBuilder()).append(Tremployeeplan.PROP_TRP_TRCP)
							.append(".").append(Trcourseplan.PROP_TRCP_TRCL_ID)
							.toString(),
					(new StringBuilder()).append(Tremployeeplan.PROP_TRP_TRCP)
							.append(".").append(Trcourseplan.PROP_TRCP_TRO_ID)
							.toString() };
			Tremployeeplan tr = tr_BO.loadById(objId, fetch);
			if (tr == null)
				return StringUtil.message(msgNoParam,
						new Object[] { "\u57F9\u8BAD\u7533\u8BF7" });
			if (!tr.getTrpSecurityNo().equals(securityNo))
				return StringUtil.message(msgSecNoErr, new Object[0]);
			tr.setTrpLastChangeBy(currentEmp);
			tr.setTrpLastChangeTime(new Date());
			obj = tr;
			nextApprover = tr.getTrpNextApprover();
			if (!Status.HR_UPD_SET.contains(tr.getTrpStatus()))
				return StringUtil.message(msgStatusErr,
						new Object[] { "\u57F9\u8BAD\u7533\u8BF7" });
		} else if ("recruitrequest".equals(flowType)) {
			IRecruitplanBo recpBo = (IRecruitplanBo) getBean("recruitplanBO");
			String fetch[] = {
					Recruitplan.PROP_RECP_DEPARTMENT_NO,
					Recruitplan.PROP_RECP_PB_NO,
					Recruitplan.PROP_RECP_WORK_LOCATION,
					Recruitplan.PROP_RECP_TYPE,
					Recruitplan.PROP_RECP_CREATE_BY,
					(new StringBuilder()).append(
							Recruitplan.PROP_RECP_CREATE_BY).append(".")
							.append(Employee.PROP_EMP_DEPT_NO).toString(),
					Recruitplan.PROP_RECP_LAST_CHANGE_BY,
					(new StringBuilder()).append(
							Recruitplan.PROP_RECP_LAST_CHANGE_BY).append(".")
							.append(Employee.PROP_EMP_DEPT_NO).toString() };
			Recruitplan recp = recpBo.loadRecruitplan(objId, fetch);
			if (recp == null)
				return StringUtil.message(msgNoParam,
						new Object[] { "\u62DB\u8058\u8BA1\u5212" });
			if (!recp.getRecpSecurityNo().equals(securityNo))
				return StringUtil.message(msgSecNoErr, new Object[0]);
			recp.setRecpLastChangeBy(currentEmp);
			recp.setRecpLastChangeTime(new Date());
			obj = recp;
			nextApprover = recp.getRecpNextApprover();
			if (!Status.HR_UPD_SET.contains(recp.getRecpStatus()))
				return StringUtil.message(msgStatusErr,
						new Object[] { "\u62DB\u8058\u8BA1\u5212" });
		}
		if (nextApprover == null) {
			operate = (new StringBuilder()).append("hr-").append(operate)
					.toString();
		} else {
			UserBo userBo = (UserBo) getBean("userService");
			Userinfo userinfo = userBo.getUserById(employeeId);
			if (userBo.checkAuthModule(userinfo, "411", "3"))
				operate = (new StringBuilder()).append("gm-").append(operate)
						.toString();
			else
				operate = (new StringBuilder()).append("mgr-").append(operate)
						.toString();
		}
		currentEmp = operatorInit(operate, obj, currentEmp, currentEmp
				.getPosition());
		AbstractWorkflow wf = null;
		if ("leaverequest".equals(flowType))
			wf = new LrWorkflow(operate, comment, obj);
		else if ("overtimerequest".equals(flowType))
			wf = new OrWorkflow(operate, comment, obj);
		else if ("trainrequest".equals(flowType))
			wf = new TrWorkflow(operate, comment, obj);
		else if ("recruitrequest".equals(flowType))
			wf = new RecWorkflow(operate, comment, obj);
		if (!AbstractWorkflow.wfRelationCheck(wf))
			if (wf.getFlowType().equals("recruitrequest"))
				return msgNoChargeRecp;
			else
				return StringUtil.message(msgNoCharge, new Object[] {
						wf.getApplier().getEmpName(), wf.getDocDesc() });
		ILeaverequestBO lr_BO = (ILeaverequestBO) getBean("leaverequestBO");
		Map msgMap = lr_BO.saveOrUpdateFlowData(new Object[] { obj });
		Iterator iterator = msgMap.keySet().iterator();
		String failMsg = "";
		String succMsg = "";
		while (iterator.hasNext()) {
			String msg = (String) iterator.next();
			String var = ((StringBuffer) msgMap.get(msg)).toString();
			if (msg.contains("\u64CD\u4F5C\u5931\u8D25"))
				failMsg = (new StringBuilder()).append(failMsg).append(var)
						.toString();
			else
				succMsg = (new StringBuilder()).append(succMsg).append(var)
						.toString();
		}
		if (failMsg.length() > 0)
			return StringUtil.message(msgFail, new Object[] { failMsg });
		else
			return StringUtil.message(msgSUCC, new Object[] { succMsg });
	}

	public String afterInit(Object obj[]) {
		return null;
	}

	public String approveOrReject(String actionName, String operate, String id,
			String logMeg, HttpSession session) {
		if ("allLeaveSearch.action".equals(actionName)) {
			if ("cancel".equals(operate))
				return lrCancelled(id, logMeg, session);
		} else if ("allOvertimeSearch.action".equals(actionName)) {
			if ("cancel".equals(operate))
				return orCancelled(id, logMeg, session);
		} else if ("trepList.action".equals(actionName)) {
			if ("cancel".equals(operate))
				return trCancelled(id, logMeg, session);
		} else if ("SearchRecruitplanForHR.action".equals(actionName)
				&& "cancel".equals(operate))
			return reCancelled(id, logMeg, session);
		return "\u9519\u8BEF\uFF01";
	}

	public String lrCancelled(String lrID, String logMsg, HttpSession session) {
		if (session == null || WebContextFactory.get() == null
				|| WebContextFactory.get().getSession() == null)
			return "\u6743\u9650\u8FC7\u671F\uFF0C\u8BF7\u91CD\u65B0\u767B\u5F55\uFF01";
		String auth = DWRUtil.checkAuth("allLeaveSearch", "execute");
		if ("error".equals(auth) || !"HR".equals(auth))
			return "\u60A8\u65E0\u6743\u6267\u884C\u6B64\u64CD\u4F5C\uFF01";
		if (logMsg == null || logMsg.trim().equals(""))
			return "\u4F5C\u5E9F\u65F6\uFF0C\u5FC5\u987B\u586B\u5199\u5907\u6CE8!";
		if (logMsg.trim().length() > 255)
			return "\u5907\u6CE8\u6700\u957F\u4E3A255\u4E2A\u5B57\u7B26\uFF01";
		ILeaverequestBO lr_BO = (ILeaverequestBO) getBean("leaverequestBO");
		Leaverequest lr = lr_BO.loadLeaverequest(lrID);
		IOvertimetxusedBO overtimetxusedBO = (IOvertimetxusedBO) getBean("overtimetxusedBO");
		overtimetxusedBO.rejectOrCancelLr(new Leaverequest[] { lr });
		if (!wfInit("hr-cancel", logMsg, new Object[] { lr })) {
			return "input";
		} else {
			lr_BO.saveOrUpdateFlowData(new Object[] { lr });
			return "suc";
		}
	}

	public String orCancelled(String orId, String logMeg, HttpSession session) {
		if (logMeg == null || logMeg.trim().length() == 0)
			return "\u5E9F\u5F03\u65F6\uFF0C\u5FC5\u987B\u586B\u5199\u5907\u6CE8";
		if (logMeg != null && logMeg.trim().length() > 255)
			return "\u5907\u6CE8\u6700\u957F\u4E3A255\u4E2A\u5B57\u7B26!";
		String auth = DWRUtil.checkAuth("allOvertimeSearch", "execute");
		if ("error".equals(auth))
			return "\u6CA1\u6709\u6743\u9650\uFF01";
		IOvertimetxusedBO overtimetxusedBO = (IOvertimetxusedBO) getBean("overtimetxusedBO");
		double sumHours = overtimetxusedBO.findOvertimerequestUsedTime(orId);
		if (sumHours != 0.0D)
			return (new StringBuilder())
					.append("\u52A0\u73ED\u7533\u8BF7\u5DF2\u6709")
					.append(sumHours)
					.append(
							"\u5C0F\u65F6\u8F6C\u4E3A\u8C03\u4F11\uFF0C\u4E0D\u5141\u8BB8\u5E9F\u5F03\uFF01")
					.toString();
		IOvertimerequestBo or_BO = (IOvertimerequestBo) getBean("overtimerequestBO");
		Overtimerequest or = or_BO.loadOvertimerequest(orId);
		if (!wfInit("hr-cancel", logMeg, new Object[] { or })) {
			return "input";
		} else {
			or_BO.saveOrUpdateFlowData(new Object[] { or });
			return "suc";
		}
	}

	public String trCancelled(String trId, String logMeg, HttpSession session) {
		if (logMeg == null || logMeg.trim().length() == 0)
			return "\u5E9F\u5F03\u65F6\uFF0C\u5FC5\u987B\u586B\u5199\u5907\u6CE8";
		if (logMeg != null && logMeg.trim().length() > 255)
			return "\u5907\u6CE8\u6700\u957F\u4E3A255\u4E2A\u5B57\u7B26!";
		String auth = DWRUtil.checkAuth("trepList", "execute");
		if ("error".equals(auth))
			return "\u6CA1\u6709\u6743\u9650\uFF01";
		ITremployeeplanBO tr_BO = (ITremployeeplanBO) getBean("tremployeeplanBO");
		String fetch[] = { Tremployeeplan.PROP_TRP_CREATE_BY,
				Tremployeeplan.PROP_TRP_LAST_CHANGE_BY,
				Tremployeeplan.PROP_TRP_TRAINEE_NO,
				Tremployeeplan.PROP_TRP_TRAINEE_DEPT };
		Tremployeeplan trep = tr_BO.loadById(trId, fetch);
		if (!wfInit("hr-cancel", logMeg, new Object[] { trep })) {
			return "input";
		} else {
			tr_BO.saveOrUpdateFlowData(new Object[] { trep });
			return "suc";
		}
	}

	public String reCancelled(String reId, String logMeg, HttpSession session) {
		if (logMeg == null || logMeg.trim().length() == 0)
			return "\u5E9F\u5F03\u65F6\uFF0C\u5FC5\u987B\u586B\u5199\u5907\u6CE8";
		if (logMeg != null && logMeg.trim().length() > 255)
			return "\u5907\u6CE8\u6700\u957F\u4E3A255\u4E2A\u5B57\u7B26!";
		String auth = DWRUtil.checkAuth("trepList", "execute");
		if ("error".equals(auth))
			return "\u6CA1\u6709\u6743\u9650\uFF01";
		IRecruitplanBo recpBo = (IRecruitplanBo) getBean("recruitplanBO");
		String fetch[] = {
				Recruitplan.PROP_RECP_DEPARTMENT_NO,
				Recruitplan.PROP_RECP_PB_NO,
				Recruitplan.PROP_RECP_WORK_LOCATION,
				Recruitplan.PROP_RECP_TYPE,
				Recruitplan.PROP_RECP_CREATE_BY,
				(new StringBuilder()).append(Recruitplan.PROP_RECP_CREATE_BY)
						.append(".").append(Employee.PROP_EMP_DEPT_NO)
						.toString(),
				Recruitplan.PROP_RECP_LAST_CHANGE_BY,
				(new StringBuilder()).append(
						Recruitplan.PROP_RECP_LAST_CHANGE_BY).append(".")
						.append(Employee.PROP_EMP_DEPT_NO).toString() };
		Recruitplan recp = recpBo.loadRecruitplan(reId, fetch);
		if (!wfInit("hr-cancel", logMeg, new Object[] { recp })) {
			return "input";
		} else {
			recpBo.saveOrUpdateFlowData(new Object[] { recp });
			return "suc";
		}
	}

	public String beforeInit(String operate, Object formBean) {
		return null;
	}

	public Leaverequest getLRContext(String lrID, HttpSession session) {
		if (session == null || WebContextFactory.get() == null
				|| WebContextFactory.get().getSession() == null)
			return null;
		ILeaverequestBO lr_BO = (ILeaverequestBO) getBean("leaverequestBO");
		Leaverequest retlr = lr_BO.loadLeaverequest(lrID);
		String currentEmpNo = ((Userinfo) session.getAttribute("userinfo"))
				.getId();
		if (DWRUtil.checkAuth("empLRAddDo", "execute").equals("OWN")
				&& !retlr.getLrEmpNo().getId().equals(currentEmpNo)) {
			return null;
		} else {
			String leaveType = DatabaseSysConfigManager.getInstance()
					.getProperty("sys.examin.leave.type");
			retlr.setApplyLRByDay(ExaminDateUtil.applyLRByDay(leaveType, retlr
					.getLrLtNo(), retlr.getLrEmpNo().getEmpShiftType()));
			return retlr;
		}
	}

	public Overtimerequest getOTContext(String otID, HttpSession session) {
		if (session == null || WebContextFactory.get() == null
				|| WebContextFactory.get().getSession() == null)
			return null;
		IOvertimerequestBo or_Bo = (IOvertimerequestBo) getBean("overtimerequestBO");
		Overtimerequest retOt = or_Bo.loadOvertimerequest(otID);
		String currentEmpNo = ((Userinfo) session.getAttribute("userinfo"))
				.getId();
		if (DWRUtil.checkAuth("empOTAddDo", "execute").equals("OWN")
				&& !retOt.getOrEmpNo().getId().equals(currentEmpNo))
			return null;
		else
			return retOt;
	}

	public String wfObjOpBatch(String operate, Object formBean,
			String objUpdateId[]) {
		return null;
	}

	public String wfObjOpSingle(String operate, Object formBean,
			String objUpdateId) {
		return null;
	}
}
